<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="com.isp.wxchat.model.WxUser" %>
<%
	String contextPath = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + contextPath + "/";

	String fromUserName = getFromUserName(request);
%>

<%!
	/**
	 * 取fromUserName
	 * 
	 * @param request
	 * @return
	 */
	private static String getFromUserName(HttpServletRequest request) {
		String namespace = null;
		String contextPath = request.getContextPath();
		String requestUri = request.getRequestURI();

		//如果没参数，就不用截了
		if (!requestUri.endsWith("/") && requestUri.indexOf("/WEB-INF/jsp/") != -1) {
			namespace = requestUri.substring(requestUri.indexOf("/WEB-INF/jsp/") + "/WEB-INF/jsp/".length());
			namespace = namespace.substring(0, namespace.indexOf("/"));
		}

		String fromUserName = (String) request.getAttribute("fromUserName");
		if (null == fromUserName || "".equals(fromUserName.trim())) {
			fromUserName = request.getParameter("fromUserName");
		}
		if (null == fromUserName || "".equals(fromUserName.trim())) {
			WxUser user = null;
			if ("master".equals(namespace))
				user = (WxUser) request.getSession().getAttribute("master");
			else if ("renter".equals(namespace))
				user = (WxUser) request.getSession().getAttribute("renter");
			if (null != user) {
				fromUserName = user.getOpenid();
			}
		}
		return fromUserName;
	}

	/**
	 * 取url中的参数，并添加fromUserName
	 * 
	 * @param request
	 * @return
	 */
	public String getParam(HttpServletRequest request) {
		String queryString = null == request.getQueryString() ? "" : request.getQueryString();
		if ("".equals(queryString)) {
			return "";
		}
		String fromUserName = getFromUserName(request);
		if (null == fromUserName) {
			return "?" + queryString;
		}
		if (queryString.indexOf("fromUserName=") > -1) {
			return queryString.replaceAll("^(.*fromUserName=)[^&]*(&.*)*$", "?$1" + fromUserName + "$2");
		}
		return "?" + queryString + "&fromUserName=" + fromUserName;
	}
%>